//
//  ViewController.m
//  54.UITextView中插入图片
//
//  Created by chenleping on 2019/1/25.
//  Copyright © 2019年 DSY. All rights reserved.
//

#import "ViewController.h"

#import "CustomTextView.h"
#import "SIXHTMLParser.h"

@interface ViewController ()
{
    NSAttributedString *attributedString;
}
@property (nonatomic, strong) CustomTextView * textView;
@property (nonatomic, strong) NSString *htmlString;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CustomTextView * textView = [[CustomTextView alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width - 20, 500)];
    [self.view addSubview:textView];
    _textView = textView;
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    
    self.textView.editable = !self.textView.editable;
    if (self.textView.editable) {
        self.textView.attributedText = attributedString;
        attributedString = nil;
    } else {
        attributedString = self.textView.attributedText;
        //  富文本转成html
        [SIXHTMLParser sync_htmlStringWithAttributedText:self.textView.attributedText
                                             orignalHtml:self.htmlString
                                    andCompletionHandler:^(NSString *html) {
                                        if(html == nil) return ;
                                        NSMutableString *mHtml = html.mutableCopy;
//                                        [mHtml replaceOccurrencesOfString:@"/>" withString:@"/>\n" options:0 range:NSMakeRange(0, mHtml.length)];
//                                        [mHtml replaceOccurrencesOfString:@"/span>" withString:@"/span>\n" options:0 range:NSMakeRange(0, mHtml.length)];
                                        self.textView.attributedText = [[NSAttributedString alloc] initWithString:mHtml.copy attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:[UIColor blackColor]}];
                                    }];
    }
}
- (IBAction)selectPic:(id)sender {
    
}


@end
